/*
 * MIT License
 *
 * Copyright (c) 2024-2048 冰羽
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package cn.star.framework.rbac.entity;

import cn.star.framework.annotation.SorterOrder;
import cn.star.framework.entity.AbstractIdEntity;
import cn.star.framework.rbac.core.IRbacUser;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonProperty.Access;
import java.util.Date;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.UniqueConstraint;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.hibernate.annotations.Comment;

/**
 * {@link IRbacUser}<br>
 *
 * @author zhaoweiping
 *     <p style='color: red'>Created on 2024-09-23 11:06:00
 * @since 3.0.0
 */
@Data
@Entity
@EqualsAndHashCode(callSuper = true)
@Table(
    name = "sys_user",
    uniqueConstraints = {
      @UniqueConstraint(columnNames = {"identity_card_"}),
      @UniqueConstraint(columnNames = {"fk_tenant_id_", "username_"}),
    })
@org.hibernate.annotations.Table(appliesTo = "sys_user", comment = "用户")
public class User extends AbstractIdEntity implements IRbacUser<Role> {

  public User() {}

  public User(String username) {
    this.username = username;
  }

  public User(String username, String password) {
    this.username = username;
    this.password = password;
  }

  /** 账号 */
  @Column(name = "username_", length = 64, nullable = false)
  @Comment(value = "账号")
  @SorterOrder
  private String username;

  /** 密码 */
  @Column(name = "password_", length = 64)
  @Comment(value = "密码")
  @JsonProperty(access = Access.WRITE_ONLY)
  private String password;

  /** 姓名 */
  @Column(name = "name_", length = 128)
  @Comment(value = "姓名")
  private String name;

  /** 性别 */
  @Column(name = "gender_", length = 16)
  @Comment(value = "性别")
  private String gender;

  /** 生日 */
  @Column(name = "birthday_")
  @Comment(value = "生日")
  @JsonFormat(pattern = "yyyy-MM-dd")
  private Date birthday;

  /** 身份证号 */
  @Column(name = "identity_card_", length = 64, unique = true)
  @Comment(value = "身份证号")
  private String identityCard;

  /** 电话号码 */
  @Column(name = "phone_", length = 32)
  @Comment(value = "电话号码")
  private String phone;

  /** 手机号码 */
  @Column(name = "mobile_", length = 32)
  @Comment(value = "手机号码")
  private String mobile;

  /** 邮箱地址 */
  @Column(name = "email_", length = 128)
  @Comment(value = "邮箱地址")
  private String email;

  /** 账号过期时间 */
  @Column(name = "account_expired_time_")
  @Comment(value = "账号过期时间")
  @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
  private Date accountExpiredTime;

  /** 是否锁定 */
  @Column(name = "account_non_locked_")
  @Comment(value = "是否锁定")
  private boolean accountNonLocked = true;

  /** 密码错误次数 */
  @Column(name = "password_error_times_")
  @Comment(value = "密码错误次数")
  private int passwordErrorTimes = 0;

  /** 密码修改时间 */
  @Column(name = "password_modify_time_")
  @Comment(value = "密码修改时间")
  @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
  private Date passwordModifyTime = new Date();

  /** 是否启用 */
  @Column(name = "enabled_")
  @Comment(value = "是否启用")
  private boolean enabled = true;

  /** 角色列表 */
  @Transient
  @JsonProperty(access = Access.WRITE_ONLY)
  private List<Role> roles;

  @Override
  @Transient
  @JsonProperty(access = Access.WRITE_ONLY)
  public List<Role> getUserIdentities() {
    return roles;
  }
}
